const cloud = require('@alipay/faas-server-sdk');
const set = [
  {
    "name":"法式蓝色芭蕾长裙夏季新款",
    "price":0.01,
    "picUrl":"https://img.alicdn.com/imgextra/i2/267096381/O1CN01y53tBo1x0Y9LWruD4_!!267096381.jpg",
    "status":"ONLIST",
    "shopeId":"01H6TE2AA5FDR1M6TQ67WR55HX",
    "shopeName":"云商城小店",
    "gmtCreate":1690440043281,
    "categoryId":"01H6B21CRJQY59T8WP1ED892Z9",
    "gmtModified":1691139422907
  },
  {
    "name": "MAMC条纹绒毛字体毛衣",
    "price": 0.01,
    "picUrl": "https://gw.alicdn.com/bao/uploaded/i1/2206542070226/O1CN01rcoGVh1DXYIeHMlyK_!!2206542070226.jpg_Q75.jpg_.webp",
    "status": "ONLIST",
    "shopeId": "01H6TE2AA5FDR1M6TQ67WR55HX",
    "shopeName": "云商城小店",
    "gmtCreate": 1690440043281,
    "categoryId": "01H6B21CRJQY59T8WP1ED892Z9",
    "gmtModified": 1691139422907
  },
  {
    "name": "摩的车灯taxi出租车灯",
    "price": 0.01,
    "picUrl": "https://gd3.alicdn.com/imgextra/i3/23732528/O1CN01p9erpB1UXs04HzxVo_!!23732528.jpg",
    "status": "ONLIST",
    "shopeId": "01H6TE2AA5FDR1M6TQ67WR55HX",
    "shopeName": "云商城小店",
    "gmtCreate": 1690440043281,
    "categoryId": "01H6B21CRJQY59T8WP1ED892Z9",
    "gmtModified": 1691139422907
  },
  {
    "name": "AZure大翻领毛毛外套",
    "price": 0.01,
    "picUrl": "https://img.alicdn.com/imgextra/i2/92675038/O1CN01FAWPeG1n5SAYNFszK_!!92675038.jpg",
    "status": "ONLIST",
    "shopeId": "01H6TE2AA5FDR1M6TQ67WR55HX",
    "shopeName": "云商城小店",
    "gmtCreate": 1690440043281,
    "categoryId": "01H6B21CRJQY59T8WP1ED892Z9",
    "gmtModified": 1691139422907
  },
  {
    "name": "南风谷【芋泥奶盖】国风新中式旗袍",
    "price": 0.01,
    "picUrl": "https://gd2.alicdn.com/imgextra/i2/97965787/O1CN01DmXp2y1scUpSbN0uq_!!97965787.jpg",
    "status": "ONLIST",
    "shopeId": "01H6TE2AA5FDR1M6TQ67WR55HX",
    "shopeName": "云商城小店",
    "gmtCreate": 1690440043281,
    "categoryId": "01H6B21CRJQY59T8WP1ED892Z9",
    "gmtModified": 1691139422907
  },
  {
    "name": "织造司原创汉服[雾花]宋制长干寺",
    "price": 0.01,
    "picUrl": "https://img.alicdn.com/imgextra/i1/1946209986/O1CN01FohgJ72NddrMABACi_!!1946209986.jpg",
    "status": "ONLIST",
    "shopeId": "01H6TE2AA5FDR1M6TQ67WR55HX",
    "shopeName": "云商城小店",
    "gmtCreate": 1690440043281,
    "categoryId": "01H6B21CRJQY59T8WP1ED892Z9",
    "gmtModified": 1691139422907
  }
]
const COLLECTION = 'mall_product';
/**
 * 初始化表数据
 */
async function createCollection(db,collection) {
  try {
    const collectionResp = await db.getCollection(collection);
    if (!collectionResp) {
      collectionResp = await db.createCollection(collection);
      return true;
    }
    console.log('collectionResp:', collectionResp)
  } catch (error) {
    console.log(error);
    return true;
  }
  return false;
}
exports.main = async (event, context) => {
  const collections = event.collections;
  if(!collections){
    return;
  }
  const db = cloud.database();
  for (const collection of collections) {
    console.log('collection:',collection)
    const resp = await createCollection(db,collection);
    console.log('resp:',resp)
    if(resp&&collection==COLLECTION){
      for (const item of set) {
        const productRsp = await db.collection(collection).add({
          data:item
        });
        console.log(`productRsp:`, productRsp);
        }
    }
  }
  return true;
};